package de.koller.umbra.render.font;

import de.koller.aframe.plain.Sprite;
import de.koller.aframe.util.misc.AColor;
import de.koller.aframe.util.resource.SpriteTileGen;
import de.koller.oceanFramework.OceanException;
import de.koller.oceanFramework.gl.util.IImageRaster;
import de.koller.oceanFramework.gl.util.ImagePreview;
import de.koller.oceanFramework.gl.util.RasterManager;
import java.awt.GraphicsConfiguration;
import java.util.HashMap;
import java.util.Map;

// todo: realy slow render process!
public class FontRender {

	public final int glyphWidht, glyphHeight;
	private final SpriteTileGen glyphs;
	private final Map<AColor, AColor> colorReplacements = new HashMap<>();
	
	public FontRender( GraphicsConfiguration gc ) {
		this( SpriteTileGen.create( "./gameData/umbra/font.png", "./gameData/umbra/font.config", gc ) );
	}

	public FontRender( SpriteTileGen glyphs ) {
		this.glyphs = glyphs;
	
		setColorReplacement( 0x00_000000, 0x00_000000 );
		setColorReplacement( 0xff_000000, 0xff_000000 );

		glyphWidht = 8;
		glyphHeight = 12;
	}
	
	public final void setColorReplacement( int src, int dst ) {
		AColor s = new AColor( src );
		colorReplacements.put( s, new AColor( dst ) );
	}

	public int getColorReplacement( int src ) {
		AColor c = colorReplacements.get( new AColor( src ) );
		if( c == null )
			throw new OceanException( "0x" + Integer.toHexString( src ) + " not defined as colorReplacement!" );
		return c.color;
	}
	
	public void drawBasicString( String str, IImageRaster r, int x, int y ) {
		for( int i = 0; i < str.length(); i++ )
			drawGlyph( str.charAt( i ) + "", r, x + i*glyphWidht, y );
	}
	
	public void drawGlyph( String gName, IImageRaster r, int x, int y ) {
		if( gName.equals( " " ) )
			gName = "space";
		
		Sprite s = glyphs.getSprite( gName );
		for( int ix = 0; ix < s.width; ix++ )
			for( int iy = 0; iy < s.height; iy++ ) {
				int c = getColorReplacement( s.raster.getColor( ix, iy ) );
				r.setColor( c, x + ix, y + iy );
			}
	}
	
}
